/**
 * 简历字段
 */
import { atom } from 'recoil';

// 持久化存储
const localStorageEffect =
  (key) =>
  ({ setSelf, onSet }) => {
    const savedValue = localStorage.getItem(key);

    if (savedValue != null) {
      setSelf(JSON.parse(savedValue));
    }

    onSet((newValue, _, isReset) => {
      isReset ? localStorage.removeItem(key) : localStorage.setItem(key, JSON.stringify(newValue));
    });
  };

// 顺序
const order = atom({
  key: 'resumeOrder',
  default: [
    {
      key: '1',
      label: '基本信息',
      isOpen: true,
      isNoChange: true,
      showInput: false,
    },
    {
      key: '2',
      label: '教育经历',
      isOpen: true,
      showInput: false,
    },
    {
      key: '3',
      label: '社区博客',
      isOpen: true,
      showInput: false,
      isEdit: true,
      isNoShow: false,
    },
    {
      key: '4',
      label: '个人优势',
      isOpen: true,
      showInput: false,
    },
    {
      key: '5',
      label: '工作经历',
      isOpen: true,
      showInput: false,
    },
    {
      key: '6',
      label: '项目经历',
      isOpen: true,
      showInput: false,
    },
  ],
  effects: [localStorageEffect('resumeOrder')],
});

// 基本信息
const baseInfo = atom({
  key: 'resumeBaseInfo',
  default: [
    {
      field: 'name',
      label: '姓名',
      value: '马云',
    },
    {
      field: 'phone',
      label: '手机号',
      value: '18888888888',
    },
    {
      field: 'mail',
      label: '邮箱',
      value: 'mayun@qq.com',
    },
    {
      field: 'sex',
      label: '性别',
      value: '男',
    },
    {
      field: 'age',
      label: '年龄',
      value: '29岁',
    },
  ],
  effects: [localStorageEffect('resumeBaseInfo')],
});

// 教育经历
const educationInfo = atom({
  key: 'resumeEducationInfo',
  default: [
    {
      field: 'school',
      label: '学校',
      value: '杭州师范大学',
    },
    {
      field: 'degree',
      label: '学历',
      value: '本科',
    },
    {
      field: 'major',
      label: '专业',
      value: '电子信息工程',
    },
    {
      field: 'start',
      label: '入学时间',
      value: '2014',
    },
    {
      field: 'end',
      label: '毕业时间',
      value: '2018',
    },
  ],
  effects: [localStorageEffect('resumeEducationInfo')],
});

// 社区博客
const shineBlogInfo = atom({
  key: 'resumeShineBlogInfo',
  default: {
    field: 'blog',
    label: '社区博客',
    value: '<p> <a href="https://why404.blog.csdn.net" target="_blank">CSDN：https://why404.blog.csdn.net</a> </p>',
  },
  effects: [localStorageEffect('resumeShineBlogInfo')],
});

// 个人优势
const advantageInfo = atom({
  key: 'resumeAdvantageInfo',
  default: {
    field: 'advantage',
    label: '个人优势',
    value: '<ol><li>个人优势</li></ol>',
  },
  effects: [localStorageEffect('resumeAdvantageInfo')],
});

// 工作经历
const workInfo = atom({
  key: 'resumeWorkInfo',
  default: [
    {
      id: 1,
      child: [
        {
          field: 'companyName1',
          label: '公司名称',
          value: '字节跳动',
        },
        {
          field: 'postName1',
          label: '岗位名称',
          value: '前端开发工程师',
        },
        {
          field: 'companyStartTime1',
          label: '开始时间',
          value: '2020',
        },
        {
          field: 'companyEndTime1',
          label: '结束时间',
          value: '2023',
        },
        {
          field: 'companyWork1',
          label: '工作概述',
          value: '<ol><li>工作概述</li></ol>',
        },
      ],
    },
  ],
  effects: [localStorageEffect('resumeWorkInfo')],
});

// 项目经历
const objectInfo = atom({
  key: 'resumeObjectInfo',
  default: [
    {
      id: 1,
      child: [
        {
          field: `companyName1`,
          label: '项目名称',
          value: '抖音平台',
        },
        {
          field: `postName1`,
          label: '项目角色',
          value: 'Web开发',
        },
        {
          field: `companyStartTime1`,
          label: '开始时间',
          value: '2020',
        },
        {
          field: `companyEndTime1`,
          label: '结束时间',
          value: '2023',
        },
        {
          field: `companyWork1`,
          label: '项目概述',
          value: '<ol><li>项目概述</li></ol>',
        },
      ],
    },
  ],
  effects: [localStorageEffect('resumeObjectInfo')],
});

export { order, baseInfo, educationInfo, shineBlogInfo, advantageInfo, workInfo, objectInfo };
